package org.leetcode.middle.leetcode2;

public class Solution
{
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);

        ListNode res = dummy;

        int carry = 0;

        while (l1!=null || l2!=null){
            int num1=l1 ==null ? 0 :l1.val;
            int num2=l2 ==null ? 0 :l2.val;
            int sum =num1+num2+carry;

            carry = sum/10;
            sum = sum%10;

            res.next = new ListNode(sum);
            if (l1!=null){
                l1=l1.next;
            }
            if (l2!=null){
                l2=l2.next;
            }
            res =res.next;
        }

        if (carry>0){
            res.next=new ListNode(carry);
        }

        return dummy.next;
    }
}


